﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PhoneBilling.Models;
using PhoneBilling.Report;
using System.IO;
using System.Collections;

namespace PhoneBilling.Controllers
{
    public class SystemController : Controller
    {
        //
        // GET: /System/

        public ActionResult Index()
        {
            if (Session["User"] == null)
            {
                return this.RedirectToAction("../Account/Login");
            }

            MenuModel model = (MenuModel)Session["Menu"];
            model.SetMenu("System");

            return View();
        }

        public ActionResult SourcePhn()
        {
            if (Session["User"] == null)
            {
                return this.RedirectToAction("../Account/Login");
            }

            MenuModel model1 = (MenuModel)Session["Menu"];
            model1.SetMenu("System", "System/SourcePhn");

            SourcePhnModel model = new SourcePhnModel();
            DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            model.起始时间 = startDate;
            model.起始时间Time = startDate;
            DateTime endDate = startDate.AddMonths(1).AddSeconds(-1);
            model.终止时间 = endDate;
            model.终止时间Time = endDate;
            model.所有话类 = true;
            model.话类List = loadAreaTypeList();
            if (model.话类 != null)
                foreach (char c in model.话类)
                    foreach (var item in model.话类List)
                    {
                        if (item.ID == c.ToString())
                            item.IsSelected = true;
                    }

            return View(model);
        }

        [HttpPost]
        public ActionResult SourcePhn(SourcePhnModel model)
        {
            if (ModelState.IsValid)
            {
                TempData["Search"] = model;
                if (model.IsExport)
                    return RedirectToAction("SourcePhnReport", "System");
                else
                    return RedirectToAction("SourcePhnResult", "System");
            }
            model.话类List = loadAreaTypeList();
            if (model.话类 != null)
                foreach (char c in model.话类)
                    foreach (var item in model.话类List)
                    {
                        if (item.ID == c.ToString())
                            item.IsSelected = true;
                    }
            // 如果我们进行到这一步时某个地方出错，则重新显示表单
            return View(model);
        }

        private List<UIAreaType> loadAreaTypeList()
        {
            SanDiDbsEntities entities = new SanDiDbsEntities();
            var list = from obj in entities.OBJDefine
                       where obj.ObjectID.Trim() == "AREATYPE"
                       orderby obj.ItemID
                       select obj;
            var areaTypeList = new List<UIAreaType>();
            foreach (var item in list)
            {
                UIAreaType areaType = new UIAreaType();
                areaType.ID = item.ItemID.Trim();
                areaType.Name = string.Format("{0}.{1}", item.ItemID.Trim(), item.ItemName.Trim());
                areaTypeList.Add(areaType);
            }

            return areaTypeList;
        }

        public ActionResult SourcePhnResult()
        {
            SourcePhnModel model = TempData["Search"] as SourcePhnModel;
            TempData["Search"] = null;
            if (model == null)
                return RedirectToAction("Error", "Shared");

            List<UISourcePhn> resultList = getSourcePhnList(model);

            return View(resultList);
        }

        public FileContentResult SourcePhnReport()
        {
            SourcePhnModel model = TempData["Search"] as SourcePhnModel;
            //List<UISourcePhn> resultList = new List<UISourcePhn>();
            List<UISourcePhn> resultList = getSourcePhnList(model);

            RptSourcePhn report = new RptSourcePhn();
            report.Source.DataSource = resultList;
            using (MemoryStream stream = new MemoryStream())
            {
                report.ExportToXls(stream);
                byte[] b = new byte[stream.Length];
                stream.Position = 0;
                stream.Read(b, 0, b.Length);
                return File(b, "application/octet-stream", "原始话单.xls");
            }

        }

        private static List<UISourcePhn> getSourcePhnList(SourcePhnModel model)
        {
            SanDiDbsEntities entities = new SanDiDbsEntities();
            entities.CommandTimeout = 600;
            DateTime startDate = new DateTime(model.起始时间.Year, model.起始时间.Month, model.起始时间.Day, model.起始时间Time.Hour, model.起始时间Time.Minute, model.起始时间Time.Second);
            DateTime endDate = new DateTime(model.终止时间.Year, model.终止时间.Month, model.终止时间.Day, model.终止时间Time.Hour, model.终止时间Time.Minute, model.终止时间Time.Second);
            var list = from p in entities.PhnToday
                       where p.EndTime >= startDate &&
                             p.EndTime <= endDate
                       select p;
            if (model.主叫分机 != null && model.主叫分机.Trim() != string.Empty)
            {
                list = from p in list
                       where p.CallerID.Trim().IndexOf(model.主叫分机.Trim()) >= 0
                       select p;
            }
            if (model.拨出号码 != null && model.拨出号码.Trim() != string.Empty)
            {
                list = from p in list
                       where p.DialNo.Trim().IndexOf(model.拨出号码.Trim()) >= 0
                       select p;
            }
            if (model.Is金额上)
            {
                list = from p in list
                       where (p.BaseFee.Value + p.ExtraFee.Value + p.ServeFee.Value) >= model.金额上
                       select p;
            }
            if (model.Is金额下)
            {
                list = from p in list
                       where (p.BaseFee.Value + p.ExtraFee.Value + p.ServeFee.Value) <= model.金额下
                       select p;
            }
            if (!model.所有话类 && model.话类 != null && model.话类 != string.Empty)
            {
                char[] temp = model.话类.ToArray();
                List<string> strs = new List<string>();
                foreach (var c in temp)
                    strs.Add(c.ToString());

                list = from p in list
                       where strs.Contains(p.AreaType.Trim())
                       select p;
            }

            list = from p in list
                   orderby p.EndTime, p.CallerID
                   select p;
            List<UISourcePhn> resultList = new List<UISourcePhn>();
            foreach (var item in list)
            {
                UISourcePhn phn = new UISourcePhn();
                phn.拨出号码 = (item.DialNo == null) ? null : item.DialNo.Trim();
                phn.主叫分机 = (item.CallerID == null) ? null : item.CallerID.Trim();
                phn.拨叫地区 = (item.AreaName == null) ? null : item.AreaName.Trim();
                phn.话类 = (item.AreaType == null) ? null : item.AreaType.Trim();
                phn.挂机时间 = item.EndTime;
                int second = 0;
                int minute = 0;
                int hour = 0;
                if (item.Duration != null)
                {
                    int x = Math.DivRem(item.Duration.Value, 60, out second);
                    hour = Math.DivRem(x, 60, out minute);
                }
                phn.通话时长 = new TimeSpan(hour, minute, second).ToString();
                phn.计次 = (item.Slices == null) ? 0 : item.Slices.Value;
                phn.话价 = (item.Price == null) ? "0" : item.Price.Value.ToString("0.00");
                phn.基本费 = (item.BaseFee == null) ? "0" : item.BaseFee.Value.ToString("0.00");
                phn.附加费 = (item.ExtraFee == null) ? "0" : item.ExtraFee.Value.ToString("0.00");
                phn.服务费 = (item.ServeFee == null) ? "0" : item.ServeFee.Value.ToString("0.00");
                phn.总金额 = (Convert.ToDecimal(phn.基本费) + Convert.ToDecimal(phn.附加费) + Convert.ToDecimal(phn.服务费)).ToString("0.00");
                phn.特别标志 = (item.Flag == null) ? null : item.Flag.Trim();
                phn.中继线号 = (item.TrunkNo == null) ? null : item.TrunkNo.Trim();

                resultList.Add(phn);
            }
            return resultList;
        }

        public ActionResult Unkown()
        {
            if (Session["User"] == null)
            {
                return this.RedirectToAction("../Account/Login");
            }

            MenuModel model1 = (MenuModel)Session["Menu"];
            model1.SetMenu("System", "System/Unkown");

            SanDiDbsEntities entities = new SanDiDbsEntities();
            var list = from u in entities.Unknown
                       orderby u.EndTime, u.CallerID
                       select u;
            List<UIUnkown> resultList = new List<UIUnkown>();
            foreach (var item in list)
            {
                UIUnkown unkown = new UIUnkown();
                unkown.拨出号码 = (item.DialNo == null) ? null : item.DialNo.Trim();
                unkown.主叫分机 = (item.CallerID == null) ? null : item.CallerID.Trim();
                unkown.话类 = (item.AreaType == null) ? null : item.AreaType.Trim();
                unkown.挂机时间 = item.EndTime;
                unkown.通话时长 = (item.Duration == null) ? 0 : item.Duration.Value;
                unkown.标志 = (item.Flag == null) ? null : item.Flag.Trim();
                unkown.交换机号 = (item.PabxID == null) ? null : item.PabxID.Trim();
                unkown.ID = Guid.NewGuid();

                resultList.Add(unkown);
            }

            return View(resultList);
        }

        public ActionResult UnkownEdit(DateTime endTime, string caller, Guid id)
        {
            SanDiDbsEntities entities = new SanDiDbsEntities();
            var item = (from u in entities.Unknown
                        where u.EndTime == endTime &&
                              u.CallerID.Trim() == caller
                        select u).SingleOrDefault();
            if (item == null)
            {
                TempData["Error"] = "没有找到数据！";
                return RedirectToAction("ErrorOnly", "Shared");
            }

            UnkownEditModel model = new UnkownEditModel();
            model.话类List = loadAreaTypeList();
            model.挂机时间 = item.EndTime;
            model.原始话单 = (item.RawBuf == null) ? null : item.RawBuf.Trim();
            model.拨出号码 = (item.DialNo == null) ? null : item.DialNo.Trim();
            model.主叫分机 = (item.CallerID == null) ? null : item.CallerID.Trim();
            model.话类 = (item.AreaType == null) ? null : item.AreaType.Trim();
            model.挂机时间 = item.EndTime;
            model.时长 = (item.Duration == null) ? 0 : item.Duration.Value;
            model.ID = id;

            return View(model);
        }

        [HttpPost]
        public ActionResult UnkownEdit(UnkownEditModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    SanDiDbsEntities entities = new SanDiDbsEntities();
                    var item = (from u in entities.Unknown
                                where u.EndTime == model.挂机时间 &&
                                      u.CallerID.Trim() == model.主叫分机
                                select u).SingleOrDefault();
                    if (item == null)
                    {
                        TempData["Error"] = "没有找到数据！";
                        return RedirectToAction("ErrorOnly", "Shared");
                    }

                    PhnToday phn = new PhnToday();
                    phn.EndTime = model.挂机时间;
                    phn.CallerID = model.主叫分机;
                    phn.DialNo = model.拨出号码;
                    phn.AreaName = model.拨叫地区;
                    phn.AreaType = model.话类;
                    phn.Price = model.话费;
                    phn.BaseFee = 0;
                    phn.ExtraFee = 0;
                    phn.ServeFee = 0;
                    phn.Duration = model.时长;
                    phn.Slices = model.通话计次;
                    phn.TrunkNo = (item.TrunkNo == null) ? null : item.TrunkNo.Trim();
                    phn.PabxID = (item.PabxID == null) ? null : item.PabxID.Trim();

                    entities.AddToPhnToday(phn);
                    entities.DeleteObject(item);

                    entities.SaveChanges();

                    return RedirectToAction("MessageOnly", "Shared");
                }
                catch (Exception e)
                {
                    TempData["Error"] = e.Message;
                    return RedirectToAction("ErrorOnly", "Shared");
                }
            }

            model.话类List = loadAreaTypeList();
            // 如果我们进行到这一步时某个地方出错，则重新显示表单
            return View(model);
        }

        public ActionResult UnkownDelete(DateTime endTime, string caller, Guid id)
        {
            try
            {
                SanDiDbsEntities entities = new SanDiDbsEntities();
                var item = (from u in entities.Unknown
                            where u.EndTime == endTime &&
                                  u.CallerID.Trim() == caller
                            select u).SingleOrDefault();
                if (item == null)
                {
                    TempData["Error"] = "没有找到数据！";
                    return RedirectToAction("ErrorOnly", "Shared");
                }

                entities.DeleteObject(item);
                entities.SaveChanges();

                return RedirectToAction("MessageOnly", "Shared");
            }
            catch (Exception e)
            {
                TempData["Error"] = e.Message;
                return RedirectToAction("ErrorOnly", "Shared");
            }
        }

        public ActionResult UnkownPatchDelete()
        {
            UnkownPatchDeleteModel model = new UnkownPatchDeleteModel();
            DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            model.起始时间 = startDate;
            model.起始时间Time = startDate;
            DateTime endDate = startDate.AddMonths(1).AddSeconds(-1);
            model.终止时间 = endDate;
            model.终止时间Time = endDate;

            return View(model);
        }

        [HttpPost]
        public ActionResult UnkownPatchDelete(UnkownPatchDeleteModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    DateTime startDate = new DateTime(model.起始时间.Year, model.起始时间.Month, model.起始时间.Day, model.起始时间Time.Hour, model.起始时间Time.Minute, model.起始时间Time.Second);
                    DateTime endDate = new DateTime(model.终止时间.Year, model.终止时间.Month, model.终止时间.Day, model.终止时间Time.Hour, model.终止时间Time.Minute, model.终止时间Time.Second);
                    SanDiDbsEntities entities = new SanDiDbsEntities();
                    var list = from u in entities.Unknown
                               where u.EndTime >= startDate &&
                                     u.EndTime <= endDate
                               select u;

                    foreach (var item in list)
                        entities.DeleteObject(item);
                    entities.SaveChanges();

                    return RedirectToAction("Message", "Shared");
                }
                catch (Exception e)
                {
                    TempData["Error"] = e.Message;
                    return RedirectToAction("Error", "Shared");
                }
            }

            return View(model);
        }

        public ActionResult UnkownPatchFee()
        {
            UnkownPatchFeeModel model = new UnkownPatchFeeModel();
            model.List = new List<UISourcePhn>();

            SanDiDbsEntities entities = new SanDiDbsEntities();
            var ukList = (from u in entities.Unknown
                          select u);

            foreach (var uk in ukList)
            {
                var extension = (from e in entities.Extensions
                                 where e.ExtNo.Trim() == uk.CallerID.Trim()
                                 select e).SingleOrDefault();
                if (extension == null)
                {
                    model.UndealCount++;
                    continue;
                }
                string areaType = null;
                if (uk.AreaType != null && uk.AreaType.Trim() != string.Empty)
                    areaType = uk.AreaType.Trim();
                Tariff tariff = null;
                var traiffList = (from t in entities.Tariff
                                  where t.TariffID.Trim() == extension.TariffID.Trim() &&
                                        uk.DialNo.Trim().StartsWith(t.AreaCode.Trim())
                                  select t).ToList();
                foreach (var item in traiffList)
                {
                    if (tariff == null)
                        tariff = item;

                    if (tariff.AreaCode.Trim().Length < item.AreaCode.Trim().Length)
                        tariff = item;
                }
                if ((areaType==null|| areaType==string.Empty) && tariff == null)
                {
                    model.UndealCount++;
                    continue;
                }

                if (tariff != null)
                    areaType = tariff.AreaType.Trim();

                PhnToday phn = new PhnToday();
                phn.EndTime = uk.EndTime;
                phn.CallerID = uk.CallerID;
                phn.DialNo = uk.DialNo;
                phn.AreaName = (tariff==null)?string.Empty:tariff.ChnAddress;
                phn.AreaType = areaType;
                phn.Duration = uk.Duration;
                phn.TrunkNo = uk.TrunkNo;
                phn.PabxID = uk.PabxID;

                UISourcePhn uiPhn = new UISourcePhn();
                uiPhn.拨出号码 = (uk.DialNo == null) ? null : uk.DialNo.Trim();
                uiPhn.主叫分机 = (uk.CallerID == null) ? null : uk.CallerID.Trim();
                uiPhn.话类 = (uk.AreaType == null) ? null : uk.AreaType.Trim();
                uiPhn.挂机时间 = uk.EndTime;
                int second = 0;
                int minute = 0;
                int hour = 0;
                if (uk.Duration != null)
                {
                    int x = Math.DivRem(uk.Duration.Value, 60, out second);
                    hour = Math.DivRem(x, 60, out minute);
                }
                uiPhn.通话时长 = new TimeSpan(hour, minute, second).ToString();
                

                if (!extension.Method.Trim().Contains(areaType))
                {
                    phn.Price = 0;
                    phn.BaseFee = 0;
                    phn.ExtraFee = 0;
                    phn.ServeFee = 0;
                    phn.Slices = 0;

                    entities.AddToPhnToday(phn);
                    entities.DeleteObject(uk);

                    uiPhn.计次 = 0;
                    uiPhn.话价 = "0";
                    model.List.Add(uiPhn);

                    continue;
                }

                if (tariff.SliceID != null && tariff.SliceID.Trim() != string.Empty)
                {
                    var sliceList = (from s in entities.Slice
                                     where s.SliceID.Trim() == tariff.SliceID.Trim() &&
                                           s.FeeType.Trim() == areaType
                                     select s).ToList();

                    if (sliceList.Count == 0)
                    {
                        phn.Price = 0;
                        if (tariff.BaseUnitSecond.Value == 0)
                            phn.BaseFee = 0;
                        else
                            phn.BaseFee = Math.Floor((decimal)uk.Duration / tariff.BaseUnitSecond.Value) * Convert.ToDecimal(tariff.BaseFee.Value);
                        phn.ExtraFee = Convert.ToDecimal(tariff.ExtraFee.Value);
                        if (tariff.ServeUnitSecond.Value == 0)
                            phn.ServeFee = 0;
                        else
                            phn.ServeFee = Math.Floor((decimal)uk.Duration / tariff.ServeUnitSecond.Value) * Convert.ToDecimal(tariff.ServiceFee.Value);
                        phn.Slices = Convert.ToInt32(Math.Floor((decimal)uk.Duration / tariff.BaseUnitSecond.Value));

                        entities.AddToPhnToday(phn);
                        entities.DeleteObject(uk);

                        uiPhn.计次 = phn.Slices.Value;
                        uiPhn.话价 = (Convert.ToDecimal(phn.Price.Value)+phn.BaseFee.Value+phn.ExtraFee.Value+phn.ServeFee.Value).ToString("0.00");
                        model.List.Add(uiPhn);

                        continue;

                    }

                    int end = 0;
                    float price = 0;
                    int 计次 = 0;
                    foreach (var slice in sliceList)
                    {
                        if (slice.ToSeconds > end)
                            end = slice.ToSeconds;
                        
                        int e1 = slice.ToSeconds;
                        if (uk.Duration < slice.ToSeconds)
                            e1 = uk.Duration.Value;
                        int temp = Convert.ToInt32(Math.Floor((decimal)(e1 - slice.FromSeconds) / slice.UnitSecond));
                        if (slice.SliceFlag.Trim() == "Y")
                            计次 += temp;
                        price += temp * slice.Fee;
                    }

                    phn.Price = price;
                    int base计次 = 0, serve计次 = 0;
                    if (uk.Duration > end)
                    {
                        if (tariff.BaseUnitSecond.Value != 0)
                            base计次 = Convert.ToInt32((decimal)(uk.Duration.Value - end) / tariff.BaseUnitSecond.Value);
                        if (tariff.ServeUnitSecond.Value != 0)
                            serve计次 = Convert.ToInt32((decimal)(uk.Duration.Value - end) / tariff.ServeUnitSecond.Value);
                    }
                    phn.BaseFee = Math.Floor(base计次 * Convert.ToDecimal(tariff.BaseFee.Value));
                    phn.ExtraFee = Convert.ToDecimal(tariff.ExtraFee.Value);
                    phn.ServeFee = Math.Floor(serve计次 * Convert.ToDecimal(tariff.ServiceFee.Value));
                    phn.Slices = base计次 + 计次;

                    entities.AddToPhnToday(phn);
                    entities.DeleteObject(uk);

                    uiPhn.计次 = phn.Slices.Value;
                    uiPhn.话价 = (Convert.ToDecimal(phn.Price.Value) + phn.BaseFee.Value + phn.ExtraFee.Value + phn.ServeFee.Value).ToString("0.00");
                    model.List.Add(uiPhn);

                    continue;
                }
                else
                {
                    phn.Price = 0;
                    if (tariff.BaseUnitSecond.Value == 0)
                        phn.BaseFee = 0;
                    else
                        phn.BaseFee = Math.Floor((decimal)uk.Duration / tariff.BaseUnitSecond.Value) * Convert.ToDecimal(tariff.BaseFee.Value);
                    phn.ExtraFee = Convert.ToDecimal(tariff.ExtraFee.Value);
                    if (tariff.ServeUnitSecond.Value == 0)
                        phn.ServeFee = 0;
                    else
                        phn.ServeFee = Math.Floor((decimal)uk.Duration / tariff.ServeUnitSecond.Value) * Convert.ToDecimal(tariff.ServiceFee.Value);
                    phn.Slices = Convert.ToInt32(Math.Floor((decimal)uk.Duration / tariff.BaseUnitSecond.Value));

                    entities.AddToPhnToday(phn);
                    entities.DeleteObject(uk);

                    uiPhn.计次 = phn.Slices.Value;
                    uiPhn.话价 = (Convert.ToDecimal(phn.Price.Value) + phn.BaseFee.Value + phn.ExtraFee.Value + phn.ServeFee.Value).ToString("0.00");
                    model.List.Add(uiPhn);

                    continue;
                }
                
            }

            entities.SaveChanges();
            return View(model);
        }

        public ActionResult ExtensionBak()
        {
            if (Session["User"] == null)
            {
                return this.RedirectToAction("../Account/Login");
            }

            MenuModel model1 = (MenuModel)Session["Menu"];
            model1.SetMenu("System", "System/ExtensionBak");

            ExtensionBakModel model = new ExtensionBakModel();
            DateTime startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            model.起始时间 = startDate;
            DateTime endDate = startDate.AddMonths(1).AddSeconds(-1);
            model.终止时间 = endDate;

            SanDiDbsEntities entities = new SanDiDbsEntities();
            var list = from e in entities.ExtensionsBak
                       orderby e.DelDatetime descending
                       select e;

            model.ExtensionList = new List<UIExtension>();
            foreach (var item in list)
            {
                UIExtension extension = new UIExtension();
                extension.分机号码 = (item.ExtNo == null) ? null : item.ExtNo.Trim();
                extension.收费等级 = (item.GradeID == null) ? null : item.GradeID.Trim();
                extension.费率 = (item.TariffID == null) ? null : item.TariffID.Trim();
                extension.收费话类 = (item.Method == null) ? null : item.Method.Trim();
                extension.报警限额 = item.Limit;
                extension.月杂费 = item.MiscFee;
                extension.用户名 = (item.UserName == null) ? null : item.UserName.Trim();
                extension.地址 = (item.Address == null) ? null : item.Address.Trim();
                extension.机架线号 = (item.LinePosition == null) ? null : item.LinePosition.Trim();
                extension.分机功能 = (item.Functions == null) ? (short)0 : item.Functions.Value;
                extension.安装日期 = item.AcctFrom.ToShortDateString();
                extension.备注 = (item.Notice == null) ? null : item.Notice.Trim();
                extension.分组 = item.Grp;
                extension.部组 = item.Department;
                extension.删除时间 = item.DelDatetime.Value;

                model.ExtensionList.Add(extension);
            }

            return View(model);
        }

        [HttpPost]
        public ActionResult ExtensionBak(ExtensionBakModel model)
        {
            if (Session["User"] == null)
            {
                return this.RedirectToAction("../Account/Login");
            }

            if(string.IsNullOrEmpty(model.分机))
                return this.RedirectToAction("../System/ExtensionBak");

            MenuModel model1 = (MenuModel)Session["Menu"];
            model1.SetMenu("System", "System/ExtensionBak");

            DateTime startTime = model.起始时间;
            DateTime endTime = model.终止时间.AddDays(1).AddMilliseconds(-5);

            SanDiDbsEntities entities = new SanDiDbsEntities();
            var list = from e in entities.ExtensionsBak
                       where e.ExtNo.Trim().IndexOf(model.分机.Trim()) >= 0 &&
                             e.DelDatetime >= startTime &&
                             e.DelDatetime <= endTime
                       orderby e.DelDatetime descending
                       select e;

            model.ExtensionList = new List<UIExtension>();
            foreach (var item in list)
            {
                UIExtension extension = new UIExtension();
                extension.分机号码 = (item.ExtNo == null) ? null : item.ExtNo.Trim();
                extension.收费等级 = (item.GradeID == null) ? null : item.GradeID.Trim();
                extension.费率 = (item.TariffID == null) ? null : item.TariffID.Trim();
                extension.收费话类 = (item.Method == null) ? null : item.Method.Trim();
                extension.报警限额 = item.Limit;
                extension.月杂费 = item.MiscFee;
                extension.用户名 = (item.UserName == null) ? null : item.UserName.Trim();
                extension.地址 = (item.Address == null) ? null : item.Address.Trim();
                extension.机架线号 = (item.LinePosition == null) ? null : item.LinePosition.Trim();
                extension.分机功能 = (item.Functions == null) ? (short)0 : item.Functions.Value;
                extension.安装日期 = item.AcctFrom.ToShortDateString();
                extension.备注 = (item.Notice == null) ? null : item.Notice.Trim();
                extension.分组 = item.Grp;
                extension.部组 = item.Department;
                extension.删除时间 = item.DelDatetime.Value;

                model.ExtensionList.Add(extension);
            }

            return View(model);
        }
    }
}
